﻿using BCL.BaseLib;
using BCL.BaseLib.Basics;
using DevicesView.Entity;
using DevicesView.Logic.App;
using Newtonsoft.Json;
using System.Linq;
using System.Text;

namespace DevicesView.Logic.User
{
    public class UserLogic
    {
        private static readonly IUserLogic logic = new XiannuanUser();


        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pass"></param>
        /// <param name="appCode"></param>
        /// <returns></returns>

        public int Login(UserConfig user)
        {
            logic.Error = 0;
            SystemUserInfo info = logic.Login(user);
            if (info == null)
                return logic.Error;

            user.UserId = info.Id.ToString();
            user.Save();
            AppResource.CurrentResource.LoginUser = info;
            AppResource.CurrentResource.Save();
            return logic.Error;
        }

        /// <summary>
        /// 根据权限ID获取权限
        /// </summary>
        /// <param name="fCode"></param>
        /// <returns></returns>
        public FunctionEntity GetFunction(string fCode)
        {
            return logic.GetFunction(fCode);
        }
        /// <summary>
        /// 判断状态
        /// </summary>
        /// <param name="fCode"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public bool GetFunction(string fCode, FunctionItemStatus status)
        {
            return logic.GetFunction(fCode,status);
        }
    }

    public interface IUserLogic
    {
        int Error { get; set; }
        SystemUserInfo Login(UserConfig user);

        /// <summary>
        /// 根据权限ID获取权限
        /// </summary>
        /// <param name="fCode"></param>
        /// <returns></returns>
        FunctionEntity GetFunction(string fCode);
        /// <summary>
        /// 判断状态
        /// </summary>
        /// <param name="fCode"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        bool GetFunction(string fCode, FunctionItemStatus status);
    }

    /// <summary>
    /// 114服务用户管理
    /// </summary>
    public class XiannuanUser : IUserLogic
    {
        private readonly string httpAdd = AppResource.LogicServerAddress;
        public int Error { get; set; }

        public FunctionEntity GetFunction(string fCode)
        {
            return BCLApplication.AppFunMunes.FirstOrDefault(o => o.AppCode == fCode);
        }
        public bool GetFunction(string fCode, FunctionItemStatus status)
        {
            return BCLApplication.AppFunMunes.Any(o => o.AppCode == fCode && o.Status==status);
        }
        public SystemUserInfo Login(UserConfig user)
        {
            SystemUserInfo dy = new SystemUserInfo { LoginName = user.LoginName, Password = user.Password };
            HttpItem hi = new HttpItem { Url = httpAdd + "systemuser/login", Method = HttpMethod.Post, ContentType = "application/json" };
            hi.Content = JsonConvert.SerializeObject(dy).GetBytes(Encoding.UTF8);
            HttpResult<SystemUserInfo> httpResult = WebLogic.GetHttpResult<SystemUserInfo>(hi);

            if (!httpResult.Success || httpResult.Result == null)
            {
                Error = httpResult.StatusCode == 204 ? 1002 : 65536;
                return null;
            }
            return httpResult.Result;
        }
    }
}
